﻿using System;
using System.Xml.Serialization;
using System.Collections;

namespace Hanhud.Web.EAuction
{
    public class CPropertyTypeAttribute : IXmlSerializable
    {
        #region Properties
        public int PTypeAttributeID { get; protected set; }
        public string Title { get; set; }
        public string FieldName { get; protected set; }
        public string UnitMeasure { get; set; }
        public bool Visible { get; set; }
        public bool IsRequired { get; set; } 
        #endregion

        #region Constructors
        public CPropertyTypeAttribute(string fieldName)
        {
            this.PTypeAttributeID = -1;
            this.Title = this.FieldName = fieldName;
            this.UnitMeasure = string.Empty;
            this.Visible = true;
            this.IsRequired = false;
        }

        public CPropertyTypeAttribute(int pTypeAttributeID, string title, string fieldName, string unitMeasure, bool visible, bool isRequired)
        {
            this.PTypeAttributeID = pTypeAttributeID;
            this.Title = title;
            this.FieldName = fieldName;
            this.UnitMeasure = unitMeasure;
            this.Visible = visible;
            this.IsRequired = isRequired;
        } 
        #endregion

        #region Override methods
        public override string ToString()
        {
            return base.ToString();
        }

        public override bool Equals(object obj)
        {
            return ((CPropertyTypeAttribute)obj).PTypeAttributeID == this.PTypeAttributeID;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        } 
        #endregion

        #region Xml serialization
        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {

        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteStartElement("propertyTypeAttribute");

            // write id
            writer.WriteStartAttribute("id");
            writer.WriteValue(this.PTypeAttributeID);

            // write properties
            writer.WriteStartElement("title");
            writer.WriteValue(this.Title);

            writer.WriteStartElement("fieldName");
            writer.WriteValue(this.FieldName);

            writer.WriteStartElement("unitMeasure");
            writer.WriteValue(this.UnitMeasure);

            writer.WriteStartElement("visible");
            writer.WriteValue(this.Visible);

            writer.WriteStartElement("required");
            writer.WriteValue(this.IsRequired);

            writer.WriteEndElement();
        } 
        #endregion
    }

    public class PropertyTypeAttributeCollection : CollectionBase, IXmlSerializable
    {
        #region Public methods
        public virtual CPropertyTypeAttribute this[int index]
        {
            get { return this[index]; }
            set { this[index] = value; }
        }

        public virtual CPropertyTypeAttribute this[string fieldName]
        {
            get { return GetPropertyTypeAttributeByName(fieldName); }
            set
            {
                for (int i = 0; i < Count; i++)
                {
                    int comparison = String.Compare((List[i] as CPropertyTypeAttribute).FieldName, fieldName, StringComparison.CurrentCultureIgnoreCase);
                    if (comparison == 0)
                    {
                        List[i] = value;
                        return;
                    }
                }

                Add(value);
            }
        }

        public new void RemoveAt(int index)
        {
            List.RemoveAt(index);
        }

        public void Remove(CPropertyTypeAttribute propertyTypeAttribute)
        {
            List.Remove(propertyTypeAttribute);
        }

        public int IndexOf(CPropertyTypeAttribute propertyTypeAttribute)
        {
            return List.IndexOf(propertyTypeAttribute);
        }

        public void Insert(int index, CPropertyTypeAttribute propertyTypeAttribute)
        {
            if (index >= Count || index < 0)
                throw new ArgumentOutOfRangeException("index", index, "Index nằm ngoài phạm vi của danh sách");

            foreach (CPropertyTypeAttribute p in List)
            {
                for (int i = 0; i < Count; i++)
                {
                    int comparison = String.Compare(p.FieldName, propertyTypeAttribute.FieldName, StringComparison.CurrentCultureIgnoreCase);
                    if (comparison == 0)
                    {
                        throw new InvalidOperationException("'" + propertyTypeAttribute.FieldName + "' đã tồn tại trong danh sách");
                    }
                }

                base.OnInsert(index, propertyTypeAttribute);
            }
        } 
        #endregion

        #region Private methods
        private void Add(CPropertyTypeAttribute value)
        {
            List.Add(value);
        }

        private CPropertyTypeAttribute GetPropertyTypeAttributeByName(string fieldName)
        {
            foreach (CPropertyTypeAttribute p in List)
                if (String.Compare(p.FieldName, fieldName, StringComparison.CurrentCultureIgnoreCase) == 0)
                    return p;

            return null;
        } 
        #endregion

        #region Xml serialization
        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteStartElement("propertyTypeAttributeCollection");

            // Write elements
            foreach (CPropertyTypeAttribute p in List)
                p.WriteXml(writer);

            writer.WriteEndElement();
        } 
        #endregion
    }

}
